var user;
apiready = function() {

	$api.getUserInf(function(u) {
		user = u;

		var monthHtml = '';
		for (var i = 1; i < 13; i++) {
			if (i < 10) {
				monthHtml += '<option value="' + i + '">0' + i + '</option>';
			} else {
				monthHtml += '<option value="' + i + '">' + i + '</option>';
			}
		}
		$('#month').append(monthHtml);

		var yearHtml = '';
		for (var j = 2015; j < 2020; j++) {
			yearHtml += '<option value="' + j + '">' + j + '</option>';
		}
		$('#year').append(yearHtml);


		var year = $("#year").val();
		var month = $("#month").val();

		var yearmonth = "";

		init();

		$("#year").change(function() {
			init();
		});

		$("#month").change(function() {
			init();
		});
	});
};

function init() {

	var year = $("#year").val();
	var month = $("#month").val();

	var yearmonth = "";

	if (year !== '' && month !== '') {
		yearmonth = year + '-' + month;
	} else if (year !== '') {
		yearmonth = year + '-';
	}

	// if (year == '' || month == '') {
	// 	$("#date_text").text("全部账单");
	// } else {
	// 	$("#date_text").text(yearmonth);
	// }


	$api.do_post('/api/property/bill/data', {
		body: JSON.stringify({
			zycode: user.zycode,
			yearmonth: yearmonth
		})
	}, function(result) {
		var list = '<div class="bill_title"><span>全部账单</span><i></i></div>';
		var yearmonth = "";
		var resultT = result;
		if (result.length > 0) {
			for (var i in result) {

				if (result[i].yearmonth + '' != yearmonth) {
					yearmonth = result[i].yearmonth;
					list += '<div class="bill_time">';
					list += result[i].yearmonth;
					list += '</div>';
				}

				list += '<ul class="pay_list">';
				if (result[i].status == '已缴')
					list += '<li><span style="color:green">' + result[i].status + '</span><b>' + result[i].project + '</b><b>￥' + result[i].qsAccount + '</b></li>';
				else
					list += '<li><span>' + result[i].status + '</span><b>' + result[i].project + '</b><b>￥' + result[i].qsAccount + '</b></li>';
				list += '</ul>';

			}


		} else {

			list += '<div class="am-u-sm-12" style="margin:0px;background: white;padding:10px;text-align:center;">无账单数据...</div>';
		}

		$("#bill_list").html(list);

	});

}